#include "Flash_user.h"
#include "globalDef.h"
#include "string.h"
#include "SPI_Flash.h"
#include "fmc_flash.h"

//清存储
uint8_t clearPara1024(void)
{
	Flash_Erase(Sector_Erase,FlashAddrOfPara512);
	return 0;
}

/*
存指定长度参数，最大1024
注意保存和提取长度要一样
返回0保存参数成功
返回1保存失败
*/
uint8_t savePara1024(uint8_t *para, uint16_t len)
{
	uint8_t DaWr[3];
	uint8_t DaRd[1050];
	
	uint8_t i, chkXor;
	
	
	DaWr[0] = 0xA0;	//标记
	DaWr[1] = 0xA2;	//标记
	
	DaWr[2] = get_chk_xorForFlash(para, len);
	chkXor = DaWr[2];
	
	for (i=0;i<3;i++)
	{
		Flash_Erase(Sector_Erase,FlashAddrOfPara512);

		Flash_Write_Data(FlashAddrOfPara512, DaWr, 2);
		Flash_Write_Data(FlashAddrOfPara512+2, para, len);
		Flash_Write_Data(FlashAddrOfPara512+2+len, DaWr+2, 1);
		
		Flash_Read_Data(FlashAddrOfPara512, DaRd, len+3);
		
		if ( (memcmp(DaRd+2, para, len) == 0) && 
				(DaRd[0] == 0xA0) &&
				(DaRd[1] == 0xA2) &&
				(DaRd[len+2] == chkXor) )
		{
			return 0;
		}
	}

	return 1;
}

/*
取指定长度参数，最大1024
注意保存和提取长度要一样
返回0读取参数成功
返回1失败
*/
uint8_t readPara1024(uint8_t * pPara, uint16_t len)
{
	uint8_t DaRd[1050];

	uint8_t i, chkXor;
	
	for (i=0;i<3;i++)
	{
		
		Flash_Read_Data(FlashAddrOfPara512, DaRd, len+3);
		chkXor = get_chk_xorForFlash(DaRd+2, len+1);
		if ( (DaRd[0] == 0xA0) &&
				(DaRd[1] == 0xA2) &&
				(0x00 == chkXor) )
		{
			memcpy(pPara, DaRd+2, len);
			return 0;
		}
	}

	return 1;
}



/*
返回0保存参数成功
返回1保存失败
*/
uint8_t savePara(uint32_t para)
{
	uint8_t DaWr[10];
	uint8_t DaRd[10];
	uint8_t DaRd2[10];
	uint8_t i;
	
	
	DaWr[0] = 0xA0;	//标记
	DaWr[1] = 0xA1;	//标记
	DaWr[2] = (uint8_t)(para>>24);
	DaWr[3] = (uint8_t)(para>>16);
	DaWr[4] = (uint8_t)(para>>8);
	DaWr[5] = (uint8_t)(para);
	
	DaWr[6] = DaWr[2] ^ DaWr[3] ^ DaWr[4] ^ DaWr[5];

	
	
	for (i=0;i<3;i++)
	{
		Flash_Erase(Sector_Erase,FlashAddrOfPara);
	
		Flash_Write_Data(FlashAddrOfPara, DaWr, 7);
		Flash_Read_Data(FlashAddrOfPara, DaRd, 7);
		
		Flash_Write_Data(FlashAddrOfPara+1024, DaWr, 7);
		Flash_Read_Data(FlashAddrOfPara+1024, DaRd2, 7);
		
		if ( (memcmp(DaWr, DaRd, 7) == 0) || 
				(memcmp(DaWr, DaRd2, 7) == 0) )
		{
			return 0;
		}
	}
	
	
	
	return 1;
}

/*
返回0读取参数成功
*/
uint8_t readPara(uint32_t * pPara)
{
	
	uint8_t DaRd[10];
	uint8_t DaRd2[10];
	uint8_t * pRdData[2];
	uint8_t i, chkXor;
	
	
	
	Flash_Read_Data(FlashAddrOfPara, DaRd, 7);
	Flash_Read_Data(FlashAddrOfPara+1024, DaRd2, 7);
	
	pRdData[0] = DaRd;
	pRdData[1] = DaRd2;
	
	
	for (i=0; i<2; i++)
	{
		if ( (pRdData[i][0] == 0xA0) && (pRdData[i][1] == 0xA1) )
		{
			chkXor = pRdData[i][2] ^ pRdData[i][3] ^ pRdData[i][4] ^ pRdData[i][5];
			
			if (chkXor == pRdData[i][6])
			{
				*pPara = pRdData[i][2]*0x1000000 + pRdData[i][3]*0x10000 + 
								pRdData[i][4]*0x100 + pRdData[i][5];
			return 0;
			}
		}
	}
	
	
	return 1;
}


/*
测试Flash, 注意测试时会把擦除所有Flash内容
Flash的读写可以参照下面例子
*/
void testAllFlash(void)
{
		#if (_EnSpiFlash && (0))
		const uint32_t testLenMax = 1024*4;
		uint32_t adr;
		uint8_t testDaWr[testLenMax];
		uint8_t testDaRd[testLenMax];
		uint8_t testDa = 0x01;
		
		printf("Flash Test Start\r\n");
		
	//对2M字节数据的测试
		for (adr = 0; adr < 1024*1024*2; adr+=1024*4)
		{
			Flash_Erase(Sector_Erase,adr);
			memset(testDaWr, testDa, testLenMax);
			testDa +=1;
			Flash_Write_Data(adr, testDaWr, testLenMax);
			Flash_Read_Data(adr, testDaRd, testLenMax);
			Flash_Erase(Sector_Erase,adr);
			if (memcmp(testDaWr, testDaRd, testLenMax)!= 0)
			{
				printf("\r\nFlash Test Err at %04x\r\n", adr);
			}
			else
			{
				printf(".");
			}
		}
		
		printf("Flash Test OK\r\n");
		#endif
		
		//测试读写512字节参数表
		#if (_EnSpiFlash && (0))
		{
			uint8_t WrTestDa[3] = {1,2,3};
			uint8_t RdTestDa[3];
			savePara1024(WrTestDa, 3);
			readPara1024(RdTestDa, 3);
			if (memcmp(RdTestDa, WrTestDa, 3) == 0)
			{
				RdTestDa[0] = 0;
			}
		}
		#endif
		
}






